package com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.base.HeaderForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.base.IFSForm;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ImportParamsAspect {
    public ImportParamsAspect(){
    }
    private String user_token;
    private Object operateObj;

    private void setParams(Object obj){
        UserCache UserCache = null;
        Method method= null;
        HeaderForm HeaderForm = new HeaderForm();
        try {
            if(IFSForm.class.isAssignableFrom(obj.getClass())){
                return ;
            }
            method = obj.getClass().getMethod("getUser_token");
            user_token = (String)method.invoke(obj);
            if(StringUtils.isBlank(user_token)){
                return;
            }else{
                UserCache = UserTokenUtil.queryUserToken(user_token);
                if(UserCache == null){
                    return;
                }
            }
            obj.getClass().getMethod("setHeaderForm", HeaderForm.class).invoke(obj,HeaderForm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        if(UserCache == null){
            return;
        }
        String[] fields = {"setFund_account","setUser_id","setAlias_name","setMobile_tel","setOp_entrust_way","setOp_station","setTerminal_type","setUser_key","setCompany_id","setSendercomp_id","setOp_branch_no","setBranch_no"};
        //是否需要用户ID
        Object need_user_id = null;
        Object is_adviser_search = null;
        try {
            need_user_id = obj.getClass().getMethod("getNeed_user_id").invoke(obj);
            is_adviser_search = obj.getClass().getMethod("getIs_adviser_search").invoke(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        for (String field : fields){
            try {
                switch (field){
                    case "setFund_account" :
                        if(StringUtils.isNotBlank(UserCache.getAccount_content()))
                            obj.getClass().getMethod("setFund_account", String.class).invoke(obj,UserCache.getAccount_content());
                        break;
                    case "setUser_id" :
                        if("0".equals(is_adviser_search.toString()) && ((need_user_id != null &&"1".equals( need_user_id.toString())) || (("101").equals(UserCache.getUser_type()) && UserCache.getBrokerVO().getUser() != null)))
                            obj.getClass().getMethod("setUser_id", String.class).invoke(obj,UserCache.getBrokerVO().getUser().getUser_id()+"");
                        break;
                    case "setAlias_name" :
                        if(UserCache.getBrokerVO().getUser() != null)
//                            obj.getClass().getMethod("setAlias_name", String.class).invoke(obj,UserCache.getBrokerVO().getUser().getAlias_name()+"");
                        break;
                    case "setMobile_tel" :
                        if(UserCache.getBrokerVO().getUser() != null)
//                            obj.getClass().getMethod("setMobile_tel", String.class).invoke(obj,UserCache.getBrokerVO().getUser().getMobile()+"");
                        break;
                    case "setOp_entrust_way" :
                        if(StringUtils.isNotBlank(UserCache.getBrokerVO().getOp_entrust_way()))
                            obj.getClass().getMethod("setOp_entrust_way", String.class).invoke(obj,UserCache.getBrokerVO().getOp_entrust_way()+"");
                        break;
                    case "setOp_station" :
                        if(StringUtils.isNotBlank(UserCache.getBrokerVO().getOp_station()))
                            obj.getClass().getMethod("setOp_station", String.class).invoke(obj,UserCache.getBrokerVO().getOp_station()+"");
                        break;
                    case "setTerminal_type" :
                        if(StringUtils.isNotBlank(UserCache.getBrokerVO().getTerminal_type()))
                            obj.getClass().getMethod("setTerminal_type", String.class).invoke(obj,UserCache.getBrokerVO().getTerminal_type()+"");
                    case "setUser_key" :
                        if(StringUtils.isNotBlank(UserCache.getBrokerVO().getUser_key()))
                            obj.getClass().getMethod("setUser_key", String.class).invoke(obj,UserCache.getBrokerVO().getUser_key()+"");
                        break;
                    case "setCompany_id" :
                        if(StringUtils.isNotBlank(UserCache.getBrokerVO().getCompany_id())){
                            HeaderForm.setCompany_id(UserCache.getBrokerVO().getCompany_id());
                        }
                        break;
                    case "setSendercomp_id" :
                        if(StringUtils.isNotBlank(UserCache.getBrokerVO().getSendercomp_id())){
                            HeaderForm.setSendercomp_id(UserCache.getBrokerVO().getSendercomp_id());
                            obj.getClass().getMethod("setSendercomp_id", String.class).invoke(obj,UserCache.getBrokerVO().getSendercomp_id()+"");
                        }
                        break;
                    case "setOp_branch_no" :
                        if(UserCache.getBrokerVO().getOp_branch_no() != 0)
                            obj.getClass().getMethod("setOp_branch_no", String.class).invoke(obj,UserCache.getBrokerVO().getOp_branch_no()+"");
                    case "setBranch_no" :
                        if(UserCache.getBrokerVO().getBranch_no() != 0)
                            obj.getClass().getMethod("setBranch_no", String.class).invoke(obj,UserCache.getBrokerVO().getBranch_no()+"");
                    break;
                }
            }catch(IllegalAccessException e){
            }catch(NoSuchMethodException e){
            }catch(InvocationTargetException e){
            }
        }

//        try {
//            obj.getClass().getMethod("setHeaderForm", HeaderForm.class).invoke(obj,HeaderForm);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        }
        System.out.println(JSON.toJSONString(obj));
    }

    public Object setRequest(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        HttpServletRequest request = null;
        //通过分析aop监听参数分析出request等信息
        for(Object obj : args){
            setParams(obj);
        }
        return pjp.proceed();
    }

}
